package com.sofaguest.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sofaguest.dao.MsgDao;
import com.sofaguest.dao.SofaDao;
import com.sofaguest.dao.TravelDao;
import com.sofaguest.entry.SofaInfo;
import com.sofaguest.entry.SofaResult;
import com.sofaguest.entry.TravelInfo;
import com.sofaguest.utils.SofaUtils;

@Service
@Transactional
public class TravelServiceImpl implements TravelService {

    @Resource
    private TravelDao travelDao;
    @Resource
    private SofaDao sofaDao;
    @Resource
    private MsgDao msgDao;

    @Override
    public SofaResult qryTravelList(int page, String byTime) {
        int start = (page - 1) * 10;
        List<TravelInfo> travels = new ArrayList<TravelInfo>();
        if ("yes".equals(byTime)) {
            travels = travelDao.qyrTravelListByTime(start);
        }
        else {
            travels = travelDao.qyrTravelList(start);
        }
        if (SofaUtils.isEmpty(travels)) {
            return new SofaResult(SofaResult.ERROR_CODE, "暂无游记", "");
        }
        return new SofaResult(SofaResult.SUCCESS_CODE, "查询成功", travels);
    }

    @Override
    public SofaResult createTravel(String author, String tatile, String tourType, String content) {
        String travelId = SofaUtils.createTravelID(); // 游记ID
        String createDate = SofaUtils.getSysDate(); // 发表时间
        TravelInfo travel = new TravelInfo();
        travel.setNote_id(travelId);
        travel.setAuthor_id(author);
        travel.setNote_tatile(tatile);
        travel.setUser_type(tourType);
        travel.setNote_content(content);
        travel.setSend_date(createDate);
        travel.setKey_words("");// 暂时未使用到关键词
        travelDao.createTravel(travel);
        return new SofaResult(SofaResult.SUCCESS_CODE, "发表成功!", "");
    }

    @Override
    public SofaResult travelDetail(String noteId) {
        List<Map> travelDetails = travelDao.getTravelDetail(noteId);
        if (SofaUtils.isEmpty(travelDetails)) {
            return new SofaResult(SofaResult.ERROR_CODE, "未获取沙旅详情,请重新登录后再尝试", "");
        }
        Map travel = travelDetails.get(0);
        String hostId = (String) travel.get("author_id");
        List<SofaInfo> sofas = sofaDao.qrySofaByHostId(hostId);
        if (SofaUtils.isEmpty(sofas)) {
            travel.put("hasSofa", "没有沙发");
        }
        else {
            travel.put("hasSofa", "有沙发");
            int useCount = 0;
            for (SofaInfo sofa : sofas) {
                useCount += sofa.getUse_count();
            }
            travel.put("useCount", useCount);
        }
        return new SofaResult(SofaResult.SUCCESS_CODE, "查询成功", travel);
    }

    @Override
    public SofaResult getMassageList(int byTimeTag) {
        List<Map> msgs = msgDao.getMassageList(byTimeTag);
        return new SofaResult(SofaResult.SUCCESS_CODE, "查询成功", msgs);
    }

    @Override
    public SofaResult getTravelList(int byTimeTag) {
        List<Map> travels = travelDao.getTravelList(byTimeTag);
        return new SofaResult(SofaResult.SUCCESS_CODE, "查询成功", travels);
    }
}
